﻿using System;
using System.Data;
using System.Windows.Forms;
using UniMana.Utils.Data;
using UniMana.Data;
using DevExpress.XtraTab;
using UniMana.Utils.Helper;

namespace UniMana.Win.Training
{
    public partial class FrmScore_SubFilterSubjectClass : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_data = new DBMain();
        //private DBScore m_Score = new DBScore();
        private Guid m_gPersonelKey =  Guid.Parse(UniMana.Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey.ToString());
        private bool m_bInputScore = true;
        private DataTable m_dtClassList = new DataTable();
        public FrmScore_SubFilterSubjectClass(bool _bInputScore =true)
        {
            InitializeComponent();
            LoadData();
            radioGroupOption.SelectedIndex = 0;
            m_bInputScore = _bInputScore;
            //if (m_bInputScore == true)
            //{
            //    radioGroupOption.Properties.Items[0].Description = "Lớp được phân công nhập điểm";
            //}
            //else
            //{
            //    radioGroupOption.Properties.Items[0].Description = "Lớp được phân công kiểm tra điểm";
            //}
        }
        public void LoadData()
        {
            lueCatalogue_Term.GetData(m_data.m_DCReadOnly.Catalogue_Terms);
            lueCatalogue_SchoolYear.GetData(m_data.m_DCReadOnly.Catalogue_SchoolYears);
            lueCatalogue_SchoolYear.Properties.NullText = "---Chọn tất cả---";
            lueCatalogue_Term.Properties.NullText = "---Chọn tất cả---";
        }
        //LookupEdit
        private void lueCatalogue_SchoolYear_EditValueChanged(object sender, EventArgs e)
        {
            //getSubjectClass();
            m_dtClassList = new DataTable();
        }
        private void lueCatalogue_Term_EditValueChanged(object sender, EventArgs e)
        {
            //getSubjectClass();
            m_dtClassList = new DataTable();
        }
        //GridView
        private void grvSubjectClass_DoubleClick(object sender, EventArgs e)
        {
            if (radioGroupOption.SelectedIndex == 0)
            {
                try
                {
                    string subjectClassKey = grvSubjectClass.GetFocusedRowCellValue(grvSubjectClass.Columns["Training_Subject_ClassKey"]).ToString();
                    FrmScore_Input f = new FrmScore_Input(Convert.ToInt32(subjectClassKey), FrmScore_Input.eFuncTions.Input);
                    try
                    {
                        foreach (XtraTabPage p in ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages)
                        {
                            if (p.Text == f.Text)
                            {
                                ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages.Remove(p);
                                break;
                            }
                        }
                    }
                    catch { }
                    TabShowFrm.ShowTabbedMdi(f, ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain);
                    this.Close();
                }
                catch { }
            }
            else if (radioGroupOption.SelectedIndex == 1)
            {
                try
                {
                    string subjectClassKey = grvSubjectClass.GetFocusedRowCellValue(grvSubjectClass.Columns["Training_Subject_ClassKey"]).ToString();
                    FrmScore_Input f = new FrmScore_Input(Convert.ToInt32(subjectClassKey), FrmScore_Input.eFuncTions.Check);
                    try
                    {
                        foreach (XtraTabPage p in ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages)
                        {
                            if (p.Text == f.Text)
                            {
                                ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages.Remove(p);
                                break;
                            }
                        }
                    }
                    catch { }
                    TabShowFrm.ShowTabbedMdi(f, ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain);
                    this.Close();
                }
                catch { }
            }
            else
            {
                try
                {
                    string subjectClassKey = grvSubjectClass.GetFocusedRowCellValue(grvSubjectClass.Columns["Training_Subject_ClassKey"]).ToString();
                    FrmScore_Input f = new FrmScore_Input(Convert.ToInt32(subjectClassKey), FrmScore_Input.eFuncTions.View);
                    try
                    {
                        foreach (XtraTabPage p in ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages)
                        {
                            if (p.Text == f.Text)
                            {
                                ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages.Remove(p);
                                break;
                            }
                        }
                    }
                    catch { }
                    TabShowFrm.ShowTabbedMdi(f, ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain);
                    this.Close();
                }
                catch { }
            }
        }
        private void grvSubjectClass_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            int rowIndex = e.RowHandle;
            if (rowIndex >= 0)
            {
                rowIndex++;
                e.Info.DisplayText = rowIndex.ToString();
            }
        }
        //Methods
        private void txtSubjectClassID_KeyDown(object sender, KeyEventArgs e)
        {
            //if (e.KeyCode == Keys.Enter)
            //{
            //    if (txtSubjectClassID != null && txtSubjectClassID.Text != "")
            //    {
            //        Training_Subject_ClassInfo infoSClass = new Training_Subject_ClassInfo(txtSubjectClassID.Text);
            //        if (infoSClass.Properties.Training_Subject_ClassKey != 0)
            //        {
            //            int _iSubjectClassKey = Convert.ToInt32(infoSClass.Properties.Training_Subject_ClassKey.ToString());
            //            //if (m_bInputScore == true)
            //            //{
            //            FrmScore_Input f = new FrmScore_Input(_iSubjectClassKey, FrmScore_Input.eFuncTions.View);
            //                try
            //                {
            //                    foreach (XtraTabPage p in ((FrmTrainingManager)Application.OpenForms["FrmTrainingManager"]).xtcMain.TabPages)
            //                    {
            //                        if (p.Text == f.Text)
            //                        {
            //                            ((FrmTrainingManager)Application.OpenForms["FrmTrainingManager"]).xtcMain.TabPages.Remove(p);
            //                            break;
            //                        }
            //                    }
            //                }
            //                catch { }

            //                TabShowFrm.ShowTabbedMdi(f, ((FrmTrainingManager)Application.OpenForms["FrmTrainingManager"]).xtcMain);
            //                this.Close();
            //            //}
            //            //else
            //            //{
            //            //    FrmScore_Checked f = new FrmScore_Checked(infoSClass.Properties.ID.ToString());
            //            //    try
            //            //    {
            //            //        foreach (XtraTabPage p in ((FrmTrainingManager)Application.OpenForms["FrmTrainingManager"]).xtcMain.TabPages)
            //            //        {
            //            //            if (p.Text == f.Text)
            //            //            {
            //            //                ((FrmTrainingManager)Application.OpenForms["FrmTrainingManager"]).xtcMain.TabPages.Remove(p);
            //            //                break;
            //            //            }
            //            //        }
            //            //    }
            //            //    catch { }

            //            //    TabShowFrm.ShowTabbedMdi(f, ((FrmTrainingManager)Application.OpenForms["FrmTrainingManager"]).xtcMain);
            //            //    this.Close();
            //            //}
                        
            //        }
            //        else
            //        {
            //            DataTable dt = m_Score.m_DCReadOnly.Training_Subject_Classes.GetLikeSubjectClassList(m_Score.m_DCReadOnly.Training_SubjectClass_ScoreTypes, txtSubjectClassID.Text).ToDataTable();
            //            gctSubjectClass.DataSource = dt;
            //        }
            //    }
            //}
        }
        private void getSubjectClass()
        {
            if (radioGroupOption.SelectedIndex == 0)
            {
                groupSubjectClassId.Visible = false;
                groupSchoolYear_Term.Visible = false;
                gctSubjectClass.DataSource = null;
                //txtSubjectClassID.Text = null;
                lueCatalogue_SchoolYear.EditValue = null;
                lueCatalogue_Term.EditValue = null;
                DateTime _dDatetimeNow = SysDateTime.Now;
                //if (m_bInputScore == true)
                //{
                m_dtClassList = new DataTable();
                m_dtClassList = m_data.m_DCReadOnly.Training_Score_TaskInputs.GetClassInTask(m_data.m_DCReadOnly.Training_Subject_Classes,
                    m_data.m_DCReadOnly.Personnel_Personnels, m_gPersonelKey, _dDatetimeNow).ToDataTable();
                gctSubjectClass.DataSource = m_dtClassList;
                //}
                //else
                //{
                //    m_dtClassList = m_data.m_DCReadOnly.Training_Score_TaskInputs.GetClassInTask(m_data.m_DCReadOnly.Training_Subject_Classes,
                //        m_data.m_DCReadOnly.Personnel_Personnels, m_gPersonelKey, _dDatetimeNow, false).ToDataTable();
                //    gctSubjectClass.DataSource = m_dtClassList;
                //}

            }
            else if (radioGroupOption.SelectedIndex == 1)
            {

                groupSubjectClassId.Visible = false;
                groupSchoolYear_Term.Visible = false;
                gctSubjectClass.DataSource = null;
                //txtSubjectClassID.Text = null;
                lueCatalogue_SchoolYear.EditValue = null;
                lueCatalogue_Term.EditValue = null;
                DateTime _dDatetimeNow = SysDateTime.Now;
                m_dtClassList = new DataTable();
                m_dtClassList = m_data.m_DCReadOnly.Training_Score_TaskInputs.GetClassInTask(m_data.m_DCReadOnly.Training_Subject_Classes,
                    m_data.m_DCReadOnly.Personnel_Personnels, m_gPersonelKey, _dDatetimeNow, false).ToDataTable();
                gctSubjectClass.DataSource = m_dtClassList;
            }
            else
            {
                gctSubjectClass.DataSource = null;
                groupSchoolYear_Term.Visible = true;
                groupSubjectClassId.Visible = true;
                //if (lueCatalogue_Term.EditValue != null && lueCatalogue_SchoolYear.EditValue != null)
                //{
                    //m_dtClassList = new DataTable();
                    //m_dtClassList = m_Score.m_DCReadOnly.Training_Subject_Classes.GetSubjectClassListByTermSchoolYear(
                    //            m_Score.m_DCReadOnly.Training_SubjectClass_ScoreTypes,
                    //            lueCatalogue_Term.GetIntValue(), lueCatalogue_SchoolYear.GetIntValue()).ToDataTable();
                    //gctSubjectClass.DataSource = m_dtClassList;
                //}
            }
        }
        private void radioGroupOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            getSubjectClass();
        }
        //Buttons
        private void btnView_Click(object sender, EventArgs e)
        {
            try
            {
                m_dtClassList = new DataTable();
                m_dtClassList = m_data.m_DCReadOnly.Training_Subject_Classes.GetSubjectClassListByTermSchoolYear(
                            m_data.m_DCReadOnly.Training_SubjectClass_ScoreTypes,
                            lueCatalogue_Term.GetIntValue(), lueCatalogue_SchoolYear.GetIntValue()).ToDataTable();
                gctSubjectClass.DataSource = m_dtClassList;
            }
            catch { }
        }
        private void btnSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (btnSearch != null && btnSearch.Text  != "")
                {
                    Training_Subject_ClassInfo infoSClass = new Training_Subject_ClassInfo(btnSearch.Text);
                    if (infoSClass.Properties.Training_Subject_ClassKey != 0)
                    {
                        int _iSubjectClassKey = Convert.ToInt32(infoSClass.Properties.Training_Subject_ClassKey.ToString());
                        FrmScore_Input f = new FrmScore_Input(_iSubjectClassKey, FrmScore_Input.eFuncTions.View);
                        try
                        {
                            foreach (XtraTabPage p in ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages)
                            {
                                if (p.Text == f.Text)
                                {
                                    ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages.Remove(p);
                                    break;
                                }
                            }
                        }
                        catch { }
                        TabShowFrm.ShowTabbedMdi(f, ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain);
                        this.Close();
                    }
                    else
                    {
                        m_dtClassList = new DataTable();
                        m_dtClassList = m_data.m_DCReadOnly.Training_Subject_Classes.GetLikeSubjectClassList(m_data.m_DCReadOnly.Training_SubjectClass_ScoreTypes,
                            btnSearch.Text).ToDataTable();
                        gctSubjectClass.DataSource = m_dtClassList;
                    }
                }
            }
        }
        private void btnSearch_Properties_Click(object sender, EventArgs e)
        {
            if (btnSearch != null && btnSearch.Text != "")
            {
                Training_Subject_ClassInfo infoSClass = new Training_Subject_ClassInfo(btnSearch.Text);
                if (infoSClass.Properties.Training_Subject_ClassKey != 0)
                {
                    int _iSubjectClassKey = Convert.ToInt32(infoSClass.Properties.Training_Subject_ClassKey.ToString());
                    FrmScore_Input f = new FrmScore_Input(_iSubjectClassKey, FrmScore_Input.eFuncTions.View);
                    try
                    {
                        foreach (XtraTabPage p in ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages)
                        {
                            if (p.Text == f.Text)
                            {
                                ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain.TabPages.Remove(p);
                                break;
                            }
                        }
                    }
                    catch { }
                    TabShowFrm.ShowTabbedMdi(f, ((FrmMain)Application.OpenForms["FrmMain"]).xtcMain);
                    this.Close();
                }
                else
                {
                    m_dtClassList = new DataTable();
                    m_dtClassList = m_data.m_DCReadOnly.Training_Subject_Classes.GetLikeSubjectClassList(m_data.m_DCReadOnly.Training_SubjectClass_ScoreTypes,
                        btnSearch.Text).ToDataTable();
                    gctSubjectClass.DataSource = m_dtClassList;
                }
            }
        }
    }
}